package com.situ.blog.dao.impl;


import com.situ.blog.dao.IUserDao;
import com.situ.blog.pojo.entity.User;
import com.situ.blog.pojo.query.UserQuery;
import com.situ.blog.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao {
    @Override
    public List<User> selectByPage(UserQuery userQuery) {
        int offset = (userQuery.getPage() - 1) * userQuery.getLimit();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<User> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,password,role,nick_name,email,phone,image,status,deleted,gmt_create,gmt_modified FROM user where 1=1 ";
            //这里面放所有搜索条件
            List<Object> queryList = new ArrayList<>();
            String queryName = userQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and name like ? ";
                queryList.add("%" + queryName + "%");
            }
            Integer roleId = userQuery.getRole();
            if (roleId != null) {
                sql += " and role=? ";
                queryList.add(roleId);
            }
            sql += " limit ?,?";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            preparedStatement.setInt(queryList.size() + 1, offset);
            preparedStatement.setInt(queryList.size() + 2, userQuery.getLimit());

            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                int role = resultSet.getInt("role");
                String nickName = resultSet.getString("nick_name");
                String email = resultSet.getString("email");
                String phone = resultSet.getString("phone");
                String image = resultSet.getString("image");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("deleted");
                java.util.Date gmtCreate = resultSet.getTimestamp("gmt_create");
                java.util.Date gmtModified = resultSet.getTimestamp("gmt_modified");
                User user = new User(id, name, password, role, nickName, email, phone, image, status, deleted, gmtCreate, gmtModified);
                list.add(user);
            }
            for (User user : list) {
                System.out.println(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return list;
    }

    @Override
    public Integer selectTotalCount(UserQuery userQuery) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int totalCount = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from user where 1=1 ";
            List<Object> queryList = new ArrayList<>();
            String queryName = userQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and name like ? ";
                queryList.add(queryName);
            }
            Integer role = userQuery.getRole();
            if (role != null) {
                sql += " and role=? ";
                queryList.add(role);
            }
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                totalCount = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalCount;
    }

    @Override
    public int deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from user where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public int add(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into user(name,password,role,nick_name,email,phone,image) values(?,?,?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getName());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setInt(3, user.getRole());
            preparedStatement.setString(4, user.getNickName());
            preparedStatement.setString(5, user.getEmail());
            preparedStatement.setString(6, user.getPhone());
            preparedStatement.setString(7, user.getImage());
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public User login(String name, String password) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,password,role,nick_name,email,phone,image,status,deleted,gmt_create,gmt_modified FROM user where name=? and password=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, password);
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                int id = resultSet.getInt("id");
                int role = resultSet.getInt("role");
                String nickName = resultSet.getString("nick_name");
                String email = resultSet.getString("email");
                String phone = resultSet.getString("phone");
                String image = resultSet.getString("image");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("deleted");
                java.util.Date gmtCreate = resultSet.getDate("gmt_create");
                java.util.Date gmtModified = resultSet.getDate("gmt_modified");
                user = new User(id, name, password, role, nickName, email, phone, image, status, deleted, gmtCreate, gmtModified);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return user;
    }

    @Override
    public int updateStatus(int id, int status) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update user set status=? where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, status);
            preparedStatement.setInt(2, id);
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
}
